<template>
  <span>确定要删除吗</span>
  <div>
    <el-button @click.stop="close">取消</el-button>
    <el-button type="primary" @click.stop="confirmDelete">
      确认
    </el-button>
  </div>
</template>

<script setup>
import { reactive } from 'vue';
import { deleteUser } from '../api/user.js'
import { DeleteProduct, DeleteCategory } from '../api/product.js'
import {DeleteRotating,DeleteEvent,DeleteBulletin} from '../api/event.js'
import showMessage from '../untils/elMessage.js';
import {DeleteOrder} from '../api/order.js'


const props = defineProps({
  deleteData: {
    type: Array,
    require: true
  }
})


const emits = defineEmits(['closeDelete', 'confirmDelete'])

const close = () => {
  emits('closeDelete')
}

const confirmDelete = () => {
  const userIDs = props.deleteData.filter(item => item.user_id).map(item => item.user_id);
  const productIDs = props.deleteData.filter(item => item.product_id).map(item => item.product_id);
  const categoryIDs = props.deleteData.filter(item => item.category_id).map(item => item.category_id);
  const orderIDs = props.deleteData.filter(item => item.order_id).map(item => item.order_id);
  const rotateIDs = props.deleteData.filter(item => item.rotate_id).map(item => item.rotate_id);
  const eventIDs = props.deleteData.filter(item => item.event_id).map(item => item.event_id);
  const bulletinIDs = props.deleteData.filter(item => item.bulletin_id).map(item => item.bulletin_id);

// console.log(userIDs,productIDs,categoryIDs,orderIDs);
  if (userIDs.length > 0) {
    deleteUserData(userIDs);
  }

  if (productIDs.length > 0) {
    deleteProductData(productIDs);
  }

  if (categoryIDs.length > 0) {
    deleteCategoryData(categoryIDs);
  }

  if (orderIDs.length > 0) {
    deleteOrderData(orderIDs);
  }

  if (rotateIDs.length > 0) {
    deleteRotating(rotateIDs);
  }
  
  if (eventIDs.length > 0) {
    deleteEvent(eventIDs);
  }

  if (bulletinIDs.length > 0) {
    deleteBulletinIDs(bulletinIDs);
  }
}

// 删除用户的数据
const deleteUserData = (userIDs) => {
  deleteUser({
    userid: userIDs
  }).then(res => {
    showMessage(res);
    console.log(1);
    emits('confirmDelete')
  });
}

// 删除商品的数据
const deleteProductData = (productIDs) => {
  DeleteProduct({
    product_id: productIDs
  }).then(res => {
    showMessage(res);
    console.log(2);
    emits('confirmDelete')
  });
}

// 删除类别的数据
const deleteCategoryData = (productIDs) => {
  DeleteCategory({
    category_id: productIDs
  }).then(res => {
    showMessage(res);
    console.log(3);
    emits('confirmDelete')
  });
}

// 删除订单的数据
const deleteOrderData = (orderIDs) => {
  DeleteOrder({
    order_id: orderIDs
  }).then(res => {
    showMessage(res);
    console.log(4);
    emits('confirmDelete')
  });
}

// 删除轮播图
const deleteRotating = (rotateIDs) => {
  DeleteRotating({
    rotate_id: rotateIDs
  }).then(res => {
    showMessage(res);
    console.log(4);
    emits('confirmDelete')
  });
}

// 删除活动
const deleteEvent = (eventIDs) => {
  DeleteEvent({
    event_id: eventIDs
  }).then(res => {
    showMessage(res);
    console.log(4);
    emits('confirmDelete')
  });
}

// 删除公告
const deleteBulletinIDs = (bulletinIDs) => {
  DeleteBulletin({
    bulletin_id: bulletinIDs
  }).then(res => {
    showMessage(res);
    console.log(4);
    emits('confirmDelete')
  });
}

</script>